package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.db.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl  implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    //保存
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    //更新
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    /**
     * select * from tabl where id in ()
     * @param userIds
     * @return
     */
    //根据用户id批量查询用户详情
    public Map<Long, UserInfo> findByIds(List<Long> userIds) {
        QueryWrapper<UserInfo> qw = new QueryWrapper<>();
        qw.in("id",userIds);
        List<UserInfo> userInfos = userInfoMapper.selectList(qw);
        //将userinfo的list集合转化为map<UserId,UserInfo>
        Map<Long, UserInfo> map = new HashMap<>();
        for (UserInfo userInfo : userInfos) {
            map.put(userInfo.getId(),userInfo);
        }
        return map;
    }

    @Override
    public IPage findAll(Integer page, Integer pagesize) {
        Page<UserInfo> pageParm = new Page(page,pagesize);
        return userInfoMapper.selectPage(pageParm,null);
    }
}
